<template>
  <el-form
    class="form-container"
    ref="child"
    label-width="100px"
    :model="formModel"
    :rules="rules"
  >
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="formModel.phone"></el-input>
    </el-form-item>
    <div>
      <el-button type="primary" @click="submit">提交</el-button>
    </div>
  </el-form>
</template>

<script>
export default {
  name: 'index',
  data () {
    const checkPhone = (rule, value, callback) => {
      const reg = /^\d{11}$/
      if (!reg.test(value)) return callback(new Error('校验不合格， 我是自定义的message'))

      callback()
    }
    return {
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: ['change', 'blur'] },
          { validator: checkPhone, message: '你好呀', trigger: ['change'] }
        ]
      },
      formModel: {
        phone: ''
      }
    }
  },
  methods: {
    submit () {
      this.$refs['child'].validate((valid, failFields) => {
        console.log(valid, failFields)
      })
    }
  }
}
</script>

